import 'package:boss/main.dart';
import 'package:boss/models/commodity.dart';
import 'package:boss/models/offers.dart';
import 'package:boss/models/sku.dart';
import 'package:boss/ui/page/offersManagement/constant/offers_constant.dart';
import 'package:boss/ui/page/offersManagement/page/plusOffersMode/page/provider/base_provider.dart';
import 'package:boss/ui/widget/form/select_form_field.dart';
import 'package:boss/ui/widget/loading/loading.dart';
import 'package:hi_common/hi_common.dart';

class DoubleOffersProvider extends BaseProvider {
  DoubleOffersProvider({Offers data}) {
    super.initData(data: data);

    if (data != null) {
      secondType = data.secondDiscountList[0].secondType;
      if (secondType == 0)
        discountRate = data.secondDiscountList[0].discountRate.toString();
      else
        lessPrice = AmountUtil.price2String(data.secondDiscountList[0].reductionCount);

      data.secondDiscountList.forEach((item) {
        var commodity = Commodity.fromJson({
          'title': item.comPropertiesDTO.title,
          'id': item.commodityId,
          'price': item.comPropertiesDTO.price,
          'unit': item.comPropertiesDTO.unit,
        });

        if (item.skuId != 0) {
          commodity.commoditySkuList = [
            Sku.fromJson({
              'skuId': item.skuId,
              'skuName': item.comPropertiesDTO.commoditySkuName,
              'skuPrice': item.comPropertiesDTO.price,
            })
          ];
        }
        commoditys.add(commodity);
      });
    }
  }

  String discountRate = '';
  List<SelectItem<int>> secondTypes = [
    SelectItem(
      title: '折扣',
      value: 0,
    ),
    SelectItem(
      title: '减免',
      value: 1,
    ),
  ];
  int secondType = 0;
  String lessPrice = '';

  setLessPrice(String value) {
    lessPrice = value;
    notifyListeners();
  }

  setSecondType(int value) {
    secondType = value;
    notifyListeners();
  }

  setDiscountRate(String value) {
    discountRate = value;
    notifyListeners();
  }

  Future<bool> addDoubleOffers() async {
    try {
      if (offerName == null || offerName == '') {
        toast('请填写优惠名称');
        return false;
      }
      if (secondType == 0 && (discountRate == null || discountRate == '')) {
        toast('请填写折扣率');
        return false;
      }
      if (secondType == 1 && (lessPrice == null || lessPrice == '')) {
        toast('请填写减免金额');
        return false;
      }
      if (commoditys.length == 0) {
        toast('请选择参与活动的菜品');
        return false;
      }

      showLoadingToast();

      Map<String, dynamic> data = {
        'commodityCustomList': commoditys
            .map((item) => {
                  'commodityId': item.id,
                  'skuId': item.sku?.skuId,
                })
            .toList(),
        'discountRate': secondType == 0 ? discountRate : null,
        'reductionCount': secondType == 1 ? AmountUtil.string2Price(lessPrice) : null,
        'secondType': secondType,
        'storeDiscountStandard': {
          'activityName': offerName,
          'activityType': activityType,
          'discountType': OffersConstant.double_discount,
          'type': OffersConstant.commodity_offers,
          'supportQrcode': 0,
        },
      };

      setDateTimeForHttpData(data);

      var result = await api.addSecondDiscount(data);

      if (!result.successful()) {
        toast(result.msg);
        return false;
      }
      toast('添加成功');
      return true;
    } catch (e) {
      toast(e.toString());
      return false;
    } finally {
      hideLoadingToast();
    }
  }
}
